/**
 *  @file AtomicOperations.h
 */
#include "AtomicOperations.h"
#if defined(WIN32)
#include <intrin.h>
#endif

namespace cppflib
{

namespace util
{

/**
 *  Increment a value atomically
 */
_DLLAPI u32_t AtomicIncrementFetch(volatile u32_t *v)
{
#if defined(WIN32)
   return _InterlockedIncrement(reinterpret_cast<volatile long *>(v));
#elif defined(linux)
   return __sync_add_and_fetch(v, 1);
#endif
}

/**
 *  Decrement a value atomically
 */
_DLLAPI u32_t AtomicDecrementFetch(volatile u32_t *v)
{
#if defined(WIN32)
   return _InterlockedDecrement(reinterpret_cast<volatile long *>(v));
#elif defined(linux)
   return __sync_sub_and_fetch(v, 1);
#endif
}

/**
 *  Read a value atomically
 */
_DLLAPI u32_t AtomicRead(volatile u32_t *v)
{
#if defined(WIN32)
   return _InterlockedXor(reinterpret_cast<volatile long *>(v), 0x0);
#elif defined(linux)
   return __sync_xor_and_fetch(v, 0x0);
#endif
}


} // end of namespace util

} // end of namespace cppflib
